package com.yanqu.road.server.gameplayer.module.activity.metaspace;

import com.yanqu.road.dao.impl.activity.metaspace.MetaSpaceRewardDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.metaspace.MetaspaceRewardData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.activity.MetaSpaceProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

public class MetaspaceModule extends GeneralModule {

    private Map<Integer, Map<Integer, MetaspaceRewardData>> metaspaceRewardDataMap = new ConcurrentHashMap<>();

    public MetaspaceModule(GamePlayer player) {
        super(player);
    }

    private AtomicInteger nextId = new AtomicInteger(0);


    public void addRewardInfo(int activityId, String reward) {
        Map<Integer, MetaspaceRewardData> dataMap = metaspaceRewardDataMap.get(activityId);
        if (dataMap == null) {
            dataMap = new ConcurrentHashMap<>();
            metaspaceRewardDataMap.put(activityId, dataMap);
        }
        MetaspaceRewardData data = new MetaspaceRewardData();
        data.setUserId(player.getUserId());
        data.setActivityId(activityId);
        data.setId(nextId.incrementAndGet());
        data.setReward(reward);
        data.setNewAdd(true);
        data.setRewardTime(System.currentTimeMillis());
        data.setAddress("");
        data.setInsertOption();
        dataMap.put(data.getId(), data);
        //下发通知客户端
        syncDataMsg();
    }

    public int fillContactInfo(int activityId, int id, long phoneNum, String address) {
        Map<Integer, MetaspaceRewardData> dataMap = metaspaceRewardDataMap.get(activityId);
        if (dataMap == null) {
            return GameErrorCode.E_META_SPACE_NO_WINNING;
        }
        MetaspaceRewardData data = dataMap.get(id);
        if (data == null) {
            return GameErrorCode.E_META_SPACE_NO_WINNING;
        }
        String phoneStr = String.valueOf(phoneNum);
        if (!Pattern.matches("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", phoneStr)) {
            //手机号码格式错误
            return GameErrorCode.E_META_SPACE_PHONE_NUM_ERROR;
        }
        data.setPhoneMum(phoneNum);
        data.setAddress(address);

        //通知客户端
        syncDataMsg();
        return 0;
    }

    public void syncDataMsg() {
        List<MetaspaceRewardData> dataList = new ArrayList<>();
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.MetaSpace.getValue());
        for (ActivityInfo activityInfo : activityInfoList) {
            int activityId = activityInfo.getActivityId();
            Map<Integer, MetaspaceRewardData> dataMap = metaspaceRewardDataMap.get(activityId);
            if (dataMap == null) {
                continue;
            }
            for (MetaspaceRewardData data : new ArrayList<>(dataMap.values())) {
                if (data.getPhoneMum() == 0) {
                    dataList.add(data);
                }
            }
            MetaSpaceProto.SyncMetaSpaceMsg.Builder syncMsg = MetaSpaceProto.SyncMetaSpaceMsg.newBuilder();
            for (MetaspaceRewardData data : dataList) {
                MetaSpaceProto.MetaSpaceDataMsg.Builder msg = MetaSpaceProto.MetaSpaceDataMsg.newBuilder();
                msg.setId(data.getId());
                msg.setReward(data.getReward());
                msg.setNewAdd(data.isNewAdd());
                data.setNewAdd(false);
                syncMsg.addData(msg);
            }
            player.sendPacket(ClientProtocol.U_META_SPACE_SYNC_DATA, syncMsg);
        }

    }

    @Override
    public boolean loadData() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.MetaSpace.getValue());
        int maxId = 0;
        for (ActivityInfo activityInfo : activityInfoList) {
            Map<Integer, MetaspaceRewardData> dataMap = new MetaSpaceRewardDaoImpl().getMetaspaceRewardData(getUserId(), activityInfo.getActivityId());
            for (Integer id : dataMap.keySet()) {
                if (id > maxId) {
                    maxId = id;
                }
            }
            metaspaceRewardDataMap.put(activityInfo.getActivityId(), dataMap);
        }
        nextId = new AtomicInteger(maxId);
        return true;
    }

    @Override
    public void loginSendMsg() {
        syncDataMsg();
    }

    @Override
    public boolean afterLoadData() {

        return true;
    }

    @Override
    public boolean saveData() {
        for (Map<Integer, MetaspaceRewardData> dataMap : new ArrayList<>(metaspaceRewardDataMap.values())) {
            for (MetaspaceRewardData data : new ArrayList<>(dataMap.values())) {
                if (data.isInsertOption()) {
                    new MetaSpaceRewardDaoImpl().add(data);
                } else if (data.isUpdateOption()) {
                    new MetaSpaceRewardDaoImpl().update(data);
                }
            }
        }
        return true;
    }
}
